import React, { Component } from 'react'
import { connect } from 'react-redux'
import { Tag, Button, Form, Input, InputNumber, Icon, Cascader, Select, AutoComplete, Spin, Modal } from 'antd';
import 'react-select/dist/react-select.css';
import table from '../../charts/table'
import { ContentHeader } from '../../wrapper'
import NewContact from './newContact';
import GoodsDetail from './goodsDetail';
import QueryTable from '../../common/queryTable';
import { actions } from '../action/action'
import { socialListTh, timeFilter, socialStatus, optionsList, subList1, subList2, profileList, userAttribute, } from '../static/index'
import {doAjaxPost} from "../../common/fetch-ajax";
import {message} from "antd/lib/index";

const FormItem = Form.Item;
const Option = Select.Option;


const attributeOption = {
    profileList: profileList,
    userAttribute: userAttribute
};
let socialListOptions = {
    title: '活动列表',
    limit: 2,
    maxPageCount: 50,
    thead: socialListTh,
    pagebar: true,
    download: false,
    search: false,
 //   placeholder: '请输入活动名称',
    setting: false,
    beforeRender: function (data, options) {
        return [
            <NAME data={data} />,
            <IMAGE data={data} />,
            <_DOING data={data} />
        ]
    },
    trigger: ['type']
};

const columns = [{
    title: '品名',
    dataIndex: 'goodsName',
}, {
    title: '图片',
    dataIndex: 'goodsPic',
    render: text => <img src={text} className="goods-img"></img>
}, {
    title: '原价',
    dataIndex: 'goodsPrice',
}, {
    title: '折扣',
    dataIndex: 'discountRate',
    render: text => text + '折'
}, {
    title: '折扣价',
    dataIndex: 'discountPrice',
}, {
    title: '上架时间',
        dataIndex: 'soldonTime',
}, {
    title: '店长推荐',
    dataIndex: 'commendStatus',
    render: text => text=="1" ? '是' : '否'
}];

class CRMReport extends Component {

    render() {
        const { location, isAddContact, triggerNewContact, goodsSearchEntity, goodsDataSourece, triggerGoodsLayer, trigerLayer, triggerGoodsDetail, ifGoodsDetail } = this.props;

        let pathname = window.location.pathname;
        let title = '活动列表';

        return <div className="userList">

            <ContentHeader title={title} icon="groups" sprite="standard-sprite" iconClassName="slds-icon-standard-contact-list" subtitle="">
                <Button type="primary" icon="plus" onClick={triggerNewContact}>新增活动</Button>
            </ContentHeader>

            <div className="content">
                <div className="row">
                    <div className="col-md-12">
                        <div className="content-body">
                            <SocialList options={socialListOptions} location={location} />
                        </div>
                    </div>
                </div>
            </div>

            {
                isAddContact ?
                    <NewContact triggerNewContact={triggerNewContact} />
                    : ''
            }
            {
                triggerGoodsLayer ?
                    <QueryTable columns={columns} dataSource={goodsDataSourece} searchEntity={goodsSearchEntity} title="商品列表"
                                trigerLayer={trigerLayer}/>
                    : ''
            }{
            ifGoodsDetail ?
                    <GoodsDetail dataSource={goodsDataSourece} title="商品详情" trigerLayer={triggerGoodsDetail}/>
                    : ''
            }

        </div >
    }
}

var _CRMReport = connect(
    function (state) {
        return {
            visits: state.visits,
            visitPage: state.visitPage,
            isHideCenterPopup: state.isHideCenterPopup,
            isHideBottomPopup: state.isHideBottomPopup,
            getVisitTime: state.getVisitTime,
            keyword: state.socialList.keyword,
            isAddContact: state.newContact.isAddContact,
            triggerGoodsLayer: state.triggerGoodsLayer,
            ifGoodsDetail: state.ifGoodsDetail,
            goodsSearchEntity: state.goodsSearchEntity,
            goodsDataSourece: state.goodsDataSourece
        }
    },
    function (dispatch) {
        return {
            setCurrentType: (type) => {
                dispatch({
                    type: 'set-cur-type',
                    val: type
                });
            },
            triggerNewContact: () => {
                dispatch({
                    type: 'trigger-add-contact'
                });
            },
            trigerLayer: () => {
                dispatch({
                    type: 'trigger-goods-layer'
                });
            },
            triggerGoodsDetail: () => {
                dispatch({
                    type: 'trigger-goodsDetail-layer'
                });
            }
        }
    }
)(CRMReport);


const _ContentHeader = connect(
    function (state) {
        return {
            socialAccount: state.socialAccount
        }
    }
)(ContentHeader)


//绑定状态到发送列表
const SocialList = connect(
    function (state) {
        return state.socialList
    },
    function (dispatch) {
        return {
            getData: (params) => {
                actions.socialListRenderData(dispatch, params);
            },
        }
    }
)(table);


//活动名称
class NAME extends Component {
    render() {
        const name = this.props.data.name;
        return <div className="preview-media-limit preview-media-limit-group">
            <p>{name}</p>
        </div>
    }
}
//活动海报
class IMAGE extends Component {
    render() {
        const imgUrl = this.props.data.imgUrl;
        return <div className="preview-media-limit preview-media-limit-group">
            <img className="active-img" src={imgUrl}></img>
        </div>
    }
}
//操作
class DOING extends Component {
    toEdit(id){
        this.props.renderData(id);
    }
    render() {

        const id = this.props.data.id;
        return <div className="preview-media-limit preview-media-limit-group">
            <Button type="primary" onClick={ ()=>this.toEdit(id) } >编辑</Button>
        </div>
    }
}

const _DOING = connect(
    state => {
        return {

        }
    },
    dispatch => {
        return {
            renderData(id) {
                let params = { id };
                actions.renderData(dispatch, params, (data)=>{
                    dispatch({
                        type: 'set-active-form-data',
                        val: data.data
                    })
                });
            }

        }
    }
)(DOING);
                    // const ids = data.data.goodsList;
                    // doAjaxPost({
                    //     url: 'https://dingdian-ppe.parllay.cn/wxserver/activity/getGoodsListByIds',
                    //     data: {
                    //         "data":{
                    //             "storeNo":user.storeNo,
                    //             "ids": ids
                    //         }
                    //     },
                    //     success: (result)=>{
                    //         let param = JSON.parse(result);
                    //         if(param.success){
                    //             param = param.result;
                    //             const ids = [];
                                // for(let g of param.data){
                                //     ids.push(g.goodsId.toString());
                                // }
                                // dispatch({
                                //     type: 'render-form-goodslist',
                                //     val: param
                                //
                                // })
                                // dispatch({
                                //     type: 'init-form-goodslist',
                                //     val: ids
                                // })
                        //         dispatch({
                        //             type: 'set-active-form-data',
                        //             val: data.data
                        //         })
                        //     }else{
                        //         message.error("数据加载失败");
                        //     }
                        // },
                        // error: (error)=>{
                        //     message.error("商品列表加载失败");
                        // }
                    // });


export default _CRMReport;






